package com.puge.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.puge.eduservice.entity.EduChapter;
import com.puge.eduservice.entity.EduVideo;
import com.puge.eduservice.entity.chapter.ChapterVo;
import com.puge.eduservice.entity.chapter.VideoVo;
import com.puge.eduservice.mapper.EduChapterMapper;
import com.puge.eduservice.service.EduChapterService;
import com.puge.eduservice.service.EduVideoService;
import com.puge.exceptionhandle.PuGeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

//    注入课程小节的VideoService类，方便调用其内的方法
    @Autowired
    private EduVideoService videoService;

//课程大纲列表，根据课程id进行获取
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
//        1，根据课程Id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter  = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
//        2，根据课程Id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

//        创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();
//        3，遍历查询章节list集合进行封装
//        遍历查询章节list集合
        for (int i = 0; i < eduChapterList.size(); i++) {
//            每个章节
            EduChapter eduChapter = eduChapterList.get(i);
//            将 eduChapter 对象值复制到 ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
//            将获取到的 ChapterVo内的每一个值都存到list集合内
            finalList.add(chapterVo);



//        创建集合，用于封装章节的小节
        List<VideoVo> videoList = new ArrayList<>();
//        4，遍历查询小节list集合进行封装
        for (int m = 0; m < eduVideoList.size(); m++) {
//            得到每个小节
            EduVideo eduVideo = eduVideoList.get(m);
//            判断  小节里面的chapterId 和章节里面的id是否一样
            if (eduVideo.getChapterId().equals(chapterVo.getId())){
//                进行封装
                VideoVo videoVo = new VideoVo();

                BeanUtils.copyProperties(eduVideo,videoVo);
//                放到小节封装集合
                videoList.add(videoVo);
            }
        }
//        封装之后的小节list集合，放到章节对象里面
            chapterVo.setChildren(videoList);
    }
    return finalList;
    }


//删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
//        根据chapterid章节id查询小节表，如果查询数据，不尽兴删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);

//        判断
        if (count >0){  //查询出小节，不进行删除
                throw new PuGeException(20001,"不能删除");
        }else { //不能查询数据，进行删除
//                删除章节
            int result = baseMapper.deleteById(chapterId);
//          当result为 >0时，返回true  result<=0时，返回false
            return result>0;
        }
    }

    //根据课程id，删除章节的方法
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}

























